package com.ibeeking.found.uaa.open.service.impl;

import com.ibeeking.gmc.global.api.common.dto.OauthClientDTO;
import com.ibeeking.gmc.global.api.feign.GlobalOpenFeignClient;
import com.ibeeking.found.uaa.open.model.OpenClientDetailsModel;
import com.ibeeking.nematos.utils.result.ResponseResult;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @ClassName OpenClientDetailsServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2020-11-16 14:39
 **/
@Service("clientDetailsServiceImpl")
public class OpenClientDetailsServiceImpl implements ClientDetailsService {

    @Resource
    private GlobalOpenFeignClient globalOpenFeignClient;

    @Override
    public OpenClientDetailsModel loadClientByClientId(String clientId) throws ClientRegistrationException {
        if (StringUtils.isEmpty(clientId)) {
            throw new OAuth2Exception("clientId不能为空");
        }
        ResponseResult<OauthClientDTO> responseResult = globalOpenFeignClient.queryByClientId(clientId);
        if (null == responseResult.getData()) {
            throw new OAuth2Exception("client客户端未授权!");
        }
        return new OpenClientDetailsModel(responseResult.getData());
    }
}
